<?php 
session_cache_expire(999999999);
if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}
if(isset($_SESSION['lang']))
$lang = $_SESSION['lang'];
if (empty($lang) || $lang == "EN") {
	include "../../value/value_en.php";
} else if($lang == "VN") {
	include "../../value/value_vn.php";
}
else {
	include "../../value/value_kr.php";
}
include '../../operations/connection.php';	
$page=1;
$rows_per_page = $_SESSION['rows_page'] ;
$page_start = ( $page - 1 ) * $rows_per_page; 
$page_end = $page * $rows_per_page;
$id_employee=$_SESSION['IDEmployee'];

$IDEmployeeSend=$_REQUEST['IDEmployeeSend'];
    $tile=$_REQUEST['titleSearch'];
    $fromdate=$_REQUEST['date1'];
    $todate=$_REQUEST['date2'];
    $search=$_REQUEST['search'];

$m1=explode(",", $_REQUEST['listid']);
for($i=0;$i<count($m1);$i++){
	$m2=explode("-", $m1[$i]);
	if($m2[2]==$id_employee){
		mysql_query("UPDATE approvedoc SET IDLabel=".$_REQUEST['movelibraryid']." WHERE IDApproveDoc ='".$m2[0]. "' AND IDEmployee ='".$id_employee. "'");
	}else{
		mysql_query("UPDATE listapprove SET IDLabel=".$_REQUEST['movelibraryid']." WHERE IDGroupApprove ='" .$m2[1]. "' AND IDEmployee ='".$id_employee. "'");
		mysql_query("UPDATE list_reference SET IDLabel=".$_REQUEST['movelibraryid']." WHERE id_group ='" .$m2[1]. "' AND id_employee ='".$id_employee."'");
	}
}
$str=$_SESSION['sortn'];
mysql_query("UPDATE listapprove SET IDLabel = IDStatus WHERE IDStatus > 10");
mysql_query("UPDATE listapprove SET IDStatus=1 WHERE IDStatus > 10");
if($search!=''){
	$sql="(SELECT a.IDEmployee,a.Tile,a.IDTypeDoc,a.IDApproveDoc,a.Timecreate,employee.FullName,a.IDGroupApprove,a.SumMoney,a.isView as is_view FROM approvedoc a,employee 
WHERE a.IDLabel=".$_REQUEST['libraryid']." AND a.IDEmployee=employee.IDEmployee ";
	if($IDEmployeeSend!='ALL'){
    		$sql .="AND a.IDEmployee='$IDEmployeeSend' ";
    	}
	$sql .="and a.Tile like'%$tile%' and a.Timecreate>'".$fromdate." 00:00:00' AND a.Timecreate<'".$todate." 23:59:59' )  
union all
(SELECT a.IDEmployee,a.Tile,a.IDTypeDoc,a.IDApproveDoc,a.Timecreate,employee.FullName,a.IDGroupApprove,a.SumMoney,la.isView as is_view FROM listapprove la,approvedoc a, employee   
WHERE la.IDLabel=".$_REQUEST['libraryid']." AND a.IDGroupApprove=la.IDGroupApprove AND la.IDEmployee='".$id_employee."' and employee.IDEmployee=a.IDEmployee  ";
	if($IDEmployeeSend!='ALL'){
    		$sql .="AND a.IDEmployee='$IDEmployeeSend' ";
    	}
	$sql .="and a.Tile like'%$tile%' and a.Timecreate>'".$fromdate." 00:00:00' AND a.Timecreate<'".$todate." 23:59:59') 
union all
(SELECT a.IDEmployee,a.Tile,a.IDTypeDoc,a.IDApproveDoc,a.Timecreate,employee.FullName,a.IDGroupApprove,a.SumMoney,lr.id_status as is_view FROM list_reference lr,approvedoc a, employee  
WHERE lr.IDLabel=".$_REQUEST['libraryid']." AND a.IDGroupApprove=lr.id_group AND lr.id_employee='".$id_employee."' and employee.IDEmployee=a.IDEmployee  ";
	if($IDEmployeeSend!='ALL'){
    		$sql .="AND a.IDEmployee='$IDEmployeeSend' ";
    	}
	$sql .="and a.Tile like'%$tile%' and a.Timecreate>'".$fromdate." 00:00:00' AND a.Timecreate<'".$todate." 23:59:59')";
    	$query= mysql_query($sql." ORDER BY ".$_SESSION['sortn']." ".$_SESSION['sort'][$_SESSION['sortn']]);
    	$number_of_page = ceil ( mysql_num_rows( $query ) / $rows_per_page ); 
}else{
	$sql="(SELECT a.IDEmployee,a.Tile,a.IDTypeDoc,a.IDApproveDoc,a.Timecreate,employee.FullName,a.IDGroupApprove,a.SumMoney,a.isView as is_view FROM approvedoc a,employee 
WHERE a.IDLabel=".$_REQUEST['libraryid']." and a.IDEmployee=employee.IDEmployee )  
union all
(SELECT a.IDEmployee,a.Tile,a.IDTypeDoc,a.IDApproveDoc,a.Timecreate,employee.FullName,a.IDGroupApprove,a.SumMoney,la.isView as is_view FROM listapprove la,approvedoc a, employee   
WHERE la.IDLabel=".$_REQUEST['libraryid']." AND a.IDGroupApprove=la.IDGroupApprove AND la.IDEmployee='".$id_employee."' and employee.IDEmployee=a.IDEmployee) 
union all
(SELECT a.IDEmployee,a.Tile,a.IDTypeDoc,a.IDApproveDoc,a.Timecreate,employee.FullName,a.IDGroupApprove,a.SumMoney,lr.id_status as is_view FROM list_reference lr,approvedoc a, employee  
WHERE lr.IDLabel=".$_REQUEST['libraryid']." AND a.IDGroupApprove=lr.id_group AND lr.id_employee='".$id_employee."' and employee.IDEmployee=a.IDEmployee)";
    	$query  = mysql_query($sql." ORDER BY ".$_SESSION['sortn']." ".$_SESSION['sort'][$_SESSION['sortn']]);
    	$number_of_page = ceil ( mysql_num_rows( $query ) / $rows_per_page ); 
}
?>
<table width="100%" class="table_th" style="height:22px;" cellspacing="0">
												<tr style="background-color:#EEE;"  >
												<td width="3%"><input type="checkbox" name="chonhet" id="chonhet" onclick="clickChonHet(this);"/></td>
												<td width="3%"><?=$l_no?></td>
												<!--td><a onclick="sort(<?=$page?>,'a.IDApproveDoc','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>','<?=$IDEmployeeSend?>')">ID</a></td-->
												<td width="20%"><?=$l_document_form?></td>
												<td width="40%"><a onclick="sort('<?=$_REQUEST['liname']?>','<?=$_REQUEST['libraryid']?>',<?=$page?>,'Tile','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>','<?=$IDEmployeeSend?>')"><?=$l_title?></a></td>
												<td width="10%"><a onclick="sort('<?=$_REQUEST['liname']?>','<?=$_REQUEST['libraryid']?>',<?=$page?>,'FullName','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>','<?=$IDEmployeeSend?>')"><?=$l_writer?></a></td>
												<td width="10%"><a onclick="sort('<?=$_REQUEST['liname']?>','<?=$_REQUEST['libraryid']?>',<?=$page?>,'Timecreate','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>','<?=$IDEmployeeSend?>')"><?=$l_created_date?></a></td>
											
												</tr>
												
												
												<?php
												
											    $i = 0; 
											    while ($row=mysql_fetch_array($query)){
											    	if ( $i >= $page_start  ) 
													{ 
												    	if($row[IDTypeDoc]==7)$document = $l_vacation_form;
												    	else if($row[IDTypeDoc]==12)$document = $l_assignment_report;
												    	else if($row[IDTypeDoc]==13)$document = $l_purchase_order;
												    	else if($row[IDTypeDoc]==14)$document = $l_daily_report;
												    	else if($row[IDTypeDoc]==15)$document = $l_business_trip_report;
												    	else if($row[IDTypeDoc]==16)$document = $l_weekly_report;
												    	else if($row[IDTypeDoc]==17)$document = $l_susqence_payment_request;
												    	else if($row['IDTypeDoc']==18)$document = $l_payment_request;
												    	else if($row[IDTypeDoc]==19)$document = $l_purchase_order1;
												?>
													<tr bgcolor="#ffffff" class="highlighter">
														<td><input type="checkbox"  name="chon" value="<?php echo $row[IDApproveDoc].'-'.$row[IDGroupApprove].'-'.$row[IDEmployee];?>" /></td>
														<td width="3%" align="center"><?=($i+1)?></td>
														<!--td width="12%" ><a href="received.php?id=<?=$row[IDApproveDoc]?>&type=<?=$row[IDTypeDoc]?>&page=<?=$page?>"><?php echo $row[IDApproveDoc];?></a></td-->
														<td width="17%" align="left"><div  align="left" ><?php echo $document;?></div></td>
														<td width="30%" align="left">
															<div  align="left" >
																<a href="library&<?=$row[IDApproveDoc]?>&<?=$row[IDTypeDoc]?>&<?=$page?>"><?=$row[Tile]?> </a>
															<p style="float:right">
															<?php 
																if($row[IDTypeDoc]==13 || $row[IDTypeDoc]==15 || $row[IDTypeDoc]==17 || $row[IDTypeDoc]==19){
																	if($row[SumMoney]>0){
																		echo " [ ".number_format($row[SumMoney])." ]";
																	}else{
																		if($row[IDTypeDoc]==15 || $row[IDTypeDoc]==13   || $row[IDTypeDoc]==19){
																			$s1="select sum(buying_document.total) as tongtien from approvedoc,buying_document where approvedoc.IDApproveDoc=buying_document.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
																			$k1=mysql_query($s1);
																			while ($r1=mysql_fetch_array($k1)){
																				if($r1[tongtien]>0){
																					echo " [ ".number_format($r1[tongtien])." ]";
																				}
																			}
																		}else if($row[IDTypeDoc]==17){
																			$s1="select sum(ap_advance.money) as tongtien from approvedoc,ap_advance where approvedoc.IDApproveDoc=ap_advance.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
																			$k1=mysql_query($s1);
																			while ($r1=mysql_fetch_array($k1)){
																				if($r1[tongtien]>0){
																					echo " [ ".number_format($r1[tongtien])." ]";
																				}
																			}
																		}
																	}
																}
															?>
															</p>
															</div>
															
														</td>
														<td width="15%"><?=$row[FullName]?></td>
														<td width="20%"><?=$row[Timecreate]?></td>
														
													</tr>
												<?php 	}
												
													$i++; 
													
													if ($i >= $page_end) 
													{ 
													break; 
													} 
												}
												
													
												?>
											
											<tr>
													<td colspan="6" class="bt_td_main">
													
													<span style="background:#fff; padding:1px; display: inline-block;">
														
														<?php if ( $number_of_page > 1 ) 
														{ 
															if ($page==1)
																$p=2;
															else if ($page==$number_of_page)
																$p=$number_of_page-1;
															else $p=$page;
															if($page!=1){?>
																<a onclick="sort('<?=$_REQUEST['liname']?>','<?=$_REQUEST['libraryid']?>',1,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>','<?=$IDEmployeeSend?>')" ><span class='button_next'><?php echo "<<<";?></span> </a>
																<a onclick="sort('<?=$_REQUEST['liname']?>','<?=$_REQUEST['libraryid']?>',<?=($page-1)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>','<?=$IDEmployeeSend?>')" ><span class='button_next'><?php echo "<<";?></span> </a>
															<?php  }
															else{
																echo "<span class='button_next'><<<</span>"; 
																echo "<span class='button_next'><<</span>";
															}
															for ( $i = $p-1; $i <= $p+1; $i++ ) {	
																	if ($i<=$number_of_page && $i>0)
																	
																		if ($i==$page){
																			echo "<span class='button_next'><b>".$i."</b></span>"; 
																		}else{
																		?>
																			<a onclick="sort('<?=$_REQUEST['liname']?>','<?=$_REQUEST['libraryid']?>',<?=$i?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>','<?=$IDEmployeeSend?>')" ><span class="button_next">
																					    
																			<?php 
																			echo $i;
																			?>
																			</span> </a>
																			<?php 
																		}
																	?>
																
															<?php 
																	}
																
																if($page!=$number_of_page){ ?>
																	<a onclick="sort('<?=$_REQUEST['liname']?>','<?=$_REQUEST['libraryid']?>',<?=($page+1)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>','<?=$IDEmployeeSend?>')"><span class='button_next'><?php echo ">>";?></span> </a>
																	<a onclick="sort('<?=$_REQUEST['liname']?>','<?=$_REQUEST['libraryid']?>',<?=($number_of_page)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>','<?=$IDEmployeeSend?>')" ><span class='button_next'><?php echo ">>>";?></span> </a>
																<?php }
																else{
																	echo "<span class='button_next'>>></span> ";
																	echo "<span class='button_next'>>>></span> ";
																}
															}
														
															
															?>
																												
													</span>
													</td>
												</tr>
											</table>
											<?php mysql_close($connection);?>